#include <gio/gio.h>
#include <stdio.h>
#include "test_glib.h"

// #define TEST_SIGNAL

static ComBobbiDemoDbus* skeleton=NULL;

static char *version = NULL;


//setVersion方法的处理函数
static gboolean on_handle_set_version(ComBobbiDemoDbus* skeleton,GDBusMethodInvocation *invocation,const gchar *str,gpointer user_data)
{
    printf("Method call: %s\n", str);

    version = g_strdup(str);

    com_bobbi_demo_dbus_complete_set_version (skeleton,invocation);

    return TRUE;
}

static gboolean on_handle_get_version(ComBobbiDemoDbus* skeleton,GDBusMethodInvocation *invocation,gpointer user_data)
{
    // printf("Method call: %s\n", str);

    com_bobbi_demo_dbus_complete_get_version (skeleton,invocation, version);

    return TRUE;
}


#if 0
typedef struct _add_1_2{
    int a;
    int b;
}add_1_2;
#endif
//setVersion方法的处理函数
static gboolean on_handle_test_add(ComBobbiDemoDbus* skeleton,GDBusMethodInvocation *invocation, int a, int b,gpointer user_data)
{
    // printf("Method call: %s\n", str);

#if 0
#if 0
    add_1_2* x = (add_1_2*)user_data;
    int r = x->a + x->b;
#else
    GVariant *v;
    g_variant_store(v, user_data);
    int a;
    int b;
    g_variant_get(v, "(ii)", &a, &b);
    int r = a + b;
#endif
#endif
    int r = a + b;
    com_bobbi_demo_dbus_complete_test_add (skeleton,invocation, r);
    return TRUE;
}


/**
 * 连接上bus daemon的回调
 **/
void on_bus_acquired (GDBusConnection *connection,const gchar *name,gpointer user_data)
{
    printf("on_bus_acquired has been invoked, name is %s\n", name);
    GError *error = NULL;
    skeleton = com_bobbi_demo_dbus_skeleton_new();
    //注意方法名，需参照Hello.c里面生成的字符串
    g_signal_connect(skeleton,"handle_test_add",G_CALLBACK(on_handle_test_add),user_data);
    g_signal_connect(skeleton, "handle-set-version", G_CALLBACK(on_handle_set_version), NULL);
    g_signal_connect(skeleton, "handle-get-version", G_CALLBACK(on_handle_get_version), NULL);
    //发布服务到总线
    g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(skeleton), connection, "/com/bobbi/demo", &error);
    if(error != NULL){                                                           
        printf("Error: Failed to export object. Reason: %s.\n", error->message);
        g_error_free(error);                                                    
    }
}

/**
 * 成功注册busName的回调
 **/
void on_bus_name_acquired (GDBusConnection *connection,const gchar *name,gpointer user_data)
{
    printf("on_bus_name_acquired has been invoked\n");
}

/**
 * busName丢失的回调，一般是server挂了？？
 **/
void on_bus_name_lost (GDBusConnection *connection,const gchar *name,gpointer user_data)
{
    printf("on_bus_name_lost !!!!\n");
}

/**
 * 测试发送信号函数，调用Hello.h自动生成的方法
 **/
#ifdef TEST_SIGNAL
static gint status_value = 1;
static gboolean emit_test_status_signal(gconstpointer p)
{
    printf("emit_test_status_signal invoked\n");
    if(skeleton != NULL)
    {
        com_yft_hello_test_emit_test_status(skeleton,status_value);
    }
    status_value++;
}
#endif

int main(int argc, char const *argv[])
{
    guint owner_id;
    GMainLoop *loop;
    #if !GLIB_CHECK_VERSION(2,35,0)
    g_type_init();
    #endif
    owner_id = g_bus_own_name(G_BUS_TYPE_SESSION,"com.bobbi.demo",
                              G_BUS_NAME_OWNER_FLAGS_NONE,
                              on_bus_acquired,on_bus_name_acquired,on_bus_name_lost,NULL,NULL);

    sleep(5);
#ifdef TEST_SIGNAL
    //测试，每3s发一次signal
    g_timeout_add(3000,(GSourceFunc)emit_test_status_signal,NULL);
#endif
    //主线程进入循环
    loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);

    g_bus_unown_name(owner_id);

    return 0;
}
